package com.admin.action;

import java.io.File;
import java.util.Date;

import javax.annotation.Resource;

import org.springframework.util.StringUtils;

import com.cheers.action.BaseAction;
import com.cheers.common.Constants;
import com.cheers.common.ResultModel;
import com.cheers.model.LuckEvent;
import com.cheers.service.LuckEventService;
import com.cheers.util.VerifyUtil;


public class ManageLuckEventAction extends BaseAction {

	private static final long serialVersionUID = -2524329657337863761L;
	
	@Resource
	private LuckEventService luckEventService;
	
	private String title;
	private String price;
	private File photo;
	private String describe;
	
	
	public String add() throws Exception {
		//验证
		if (StringUtils.isEmpty(title)) {
			result = new ResultModel(Constants.NO, "活动名称不能为空", "");
			return SUCCESS;
		}
		if (StringUtils.isEmpty(price)) {
			result = new ResultModel(Constants.NO, "活动价格不能为空", "");
			return SUCCESS;
		}
		if (photo==null || photo.length()==0) {
			result = new ResultModel(Constants.NO, "活动图片不能为空", "");
			return SUCCESS;
		}
		if (!VerifyUtil.numberTest(price)) {
			result = new ResultModel(Constants.NO, "价格应为正整数", "");
			return SUCCESS;
		}
		if (!isPost("add")) {
			result = new ResultModel(Constants.NO, Constants.INVALID_DESC, "");
			return SUCCESS;
		}
		
		//构造实体
		double priceDou = Double.parseDouble(price);
		LuckEvent luckEvent = new LuckEvent();
		luckEvent.setTitle(title);
		luckEvent.setPrice(priceDou);
		//上传图片 
		String picture = upload(photo);
		luckEvent.setPicture(picture);
		luckEvent.setDepict(describe);
		//一元兑换多少积分 TODO
		luckEvent.setBuyed(10);
		luckEvent.setCtime(new Date());
		luckEvent.setTotal((int)(priceDou/1));
		luckEvent.setJoins(0);
		luckEvent.setStatus(0);
		
		//添加幸运活动
		try {
			luckEventService.add(luckEvent);
		} catch (Exception e) {
			result = new ResultModel(Constants.NO, Constants.NO_DESC, "");
			return SUCCESS;
		}
		result = new ResultModel(Constants.OK, Constants.OK_DESC, "");
		return SUCCESS;
	}
	//上传图片 TODO
	private String upload(File photo) throws Exception {
		return "/images/20140122160014868.jpg";
	}


	public String getTitle() {
		return title;
	}
	public void setTitle(String title) {
		this.title = title;
	}
	public String getPrice() {
		return price;
	}
	public void setPrice(String price) {
		this.price = price;
	}
	public File getPhoto() {
		return photo;
	}
	public void setPhoto(File photo) {
		this.photo = photo;
	}
	public String getDescribe() {
		return describe;
	}
	public void setDescribe(String describe) {
		this.describe = describe;
	}
	
}
